﻿using System.Data;
using KacincisinProject.Data;
using KacincisinProject.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KacincisinProject.Controllers
{
    public class SinavController : Controller
    {
        private KacincisinDBEntities database = new KacincisinDBEntities();
        //
        // GET: /Sinav/
        public ActionResult SinavListele()
        {
            List<Sinavlar> sinavlar= database.Sinavlar.Where(item => !item.IsDeleted).ToList();
            return View(sinavlar);
        }

        public ActionResult SinavOlustur()
        {
            ViewBag.SinavTipleri = new SelectList(database.SinavTipleri, "SinavTipId", "SinavTipi");

            return View();
        }

        [HttpPost]
        public ActionResult SinavOlustur(SinavViewModels model)
        {
            if (ModelState.IsValid)
            {
                Sinavlar yenisinav = new Sinavlar();
                yenisinav.SinavTipId = model.SinavTipId;
                yenisinav.SinavAdi = model.SinavAdi;
                yenisinav.SinavSuresi = model.SinavSuresi;
                yenisinav.SinavBaslangicTarihi = model.SinavBaslangiçTarihi;
                yenisinav.CreatedBy = 1;
                yenisinav.CreatedDate = DateTime.Now;
                

                database.Sinavlar.Add(yenisinav);
                database.SaveChanges();
                return RedirectToAction("SinavListele");
            }
            ViewBag.SinavTipleri = new SelectList(database.SinavTipleri, "SinavTipId", "SinavTipi", model.SinavTipId);
            return View(model);






           }
        public ActionResult SinavGuncelle(int id)
        {
            Sinavlar sinav = database.Sinavlar.Find(id);
            ViewBag.SinavTipleri = new SelectList(database.SinavTipleri, "SinavTipId", "SinavTipi", sinav.SinavTipId);
            SinavViewModels model = new SinavViewModels();

            
            model.SinavId = sinav.SinavId;
            model.SinavTipId = sinav.SinavTipId;
            model.SinavAdi = sinav.SinavAdi;
            model.SinavSuresi = sinav.SinavSuresi;
            model.SinavBaslangiçTarihi = sinav.SinavBaslangicTarihi;
            model.CreatedBy = sinav.CreatedBy;
            model.CreatedDate = sinav.CreatedDate;
            return View(model);

        }
        [HttpPost]
        public ActionResult SinavGuncelle(SinavViewModels model)
        {
            if (ModelState.IsValid)
            {
                Sinavlar yenisinav = database.Sinavlar.Find(model.SinavId);

                
                yenisinav.SinavTipId= model.SinavTipId;
                yenisinav.SinavAdi = model.SinavAdi;
                yenisinav.SinavSuresi = model.SinavSuresi;
                yenisinav.SinavBaslangicTarihi = model.SinavBaslangiçTarihi;
                yenisinav.IsUpdated = true;
                yenisinav.UpdatedBy = 1;
                yenisinav.UpdatedDate = DateTime.Now;



                database.Entry(yenisinav).State = EntityState.Modified;
                database.SaveChanges();

                return RedirectToAction("SinavListele");
            }

            return View(model);

        }
        public ActionResult SinavSil(int id)
        {
            Sinavlar sinav = database.Sinavlar.Find(id);
            SinavViewModels model = new SinavViewModels();

            model.SinavId=sinav.SinavId;
            model.SinavTipId = sinav.SinavTipId;
            model.SinavAdi = sinav.SinavAdi;
            model.SinavSuresi = sinav.SinavSuresi;
            model.SinavBaslangiçTarihi = sinav.SinavBaslangicTarihi;
            model.CreatedBy = sinav.CreatedBy;
            model.CreatedDate = sinav.CreatedDate;
            return View(model);
        }

        [HttpPost]
        public ActionResult SinavSil(SinavViewModels model)
        {
            if (ModelState.IsValid)
            {
                Sinavlar yenisinav = database.Sinavlar.Find(model.SinavId);

                yenisinav.IsDeleted = true;
                yenisinav.DeletedBy = 1;
                yenisinav.DeletedDate = DateTime.Now;

             


                database.Entry(yenisinav).State = EntityState.Modified;
                database.SaveChanges();
                return RedirectToAction("SinavListele");
            }

            return View(model);
        }

        public ActionResult SinavaBasla(int id)
        {
            List<SinavSoruRelationShip> sinavSoruRelationShips = database.SinavSoruRelationShip.Where(item => item.SinavId == id).ToList();
            return View(sinavSoruRelationShips);
        }
    }
}